/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.forecast.service;

import com.fzhucc.modules.forecast.domain.RaceForecast;
import com.fzhucc.modules.forecast.domain.RePre;
import com.fzhucc.modules.forecast.domain.dto.*;
import com.fzhucc.utils.*;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.forecast.repository.RePreRepository;
import com.fzhucc.modules.forecast.service.mapstruct.RePreMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.sql.Timestamp;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2023-11-22
**/
@Service
@RequiredArgsConstructor
public class RePreService {

    private final RePreRepository rePreRepository;
    private final RePreMapper rePreMapper;

    public Map<String,Object> queryAll(RePreQueryCriteria criteria, Pageable pageable){
        Page<RePre> page = rePreRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(rePreMapper::toDto));
    }

    public List<RePreDto> queryAll(RePreQueryCriteria criteria){
        return rePreMapper.toDto(rePreRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public RePreDto findById(Integer id) {
        RePre rePre = rePreRepository.findById(id).orElseGet(RePre::new);
        ValidationUtil.isNull(rePre.getId(),"RePre","id",id);
        return rePreMapper.toDto(rePre);
    }

    @Transactional(rollbackFor = Exception.class)
    public RePreDto create(RePre resources) {
        return rePreMapper.toDto(rePreRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(RePre resources) {
        RePre rePre = rePreRepository.findById(resources.getId()).orElseGet(RePre::new);
        ValidationUtil.isNull( rePre.getId(),"RePre","id",resources.getId());
        rePre.copy(resources);
        rePreRepository.save(rePre);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            rePreRepository.deleteById(id);
        }
    }

    public void download(List<RePreDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RePreDto rePre : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("需求名称", rePre.getName());
            map.put("所在单位", rePre.getCompany());
            map.put("联系方式", rePre.getPhone());
            map.put("需求类型", rePre.getType());
            map.put("需求简介", rePre.getIntro());
            map.put("企业简介", rePre.getCIntro());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    public RePreDto forecast(ForecastReDto dto) {
        RePre rePre = new RePre();
        rePre.setType(dto.getType());
        rePre.setName(dto.getName());
        rePre.setPhone(dto.getPhone());
        rePre.setIntro(dto.getIntro());
        rePre.setCompany(dto.getCompany());
        rePre.setCIntro(dto.getCIntro());
        RePre save = rePreRepository.save(rePre);
        try {
            //同时注册到助企行系统
            reigstRace(save.getName(), save.getPhone());
        } catch (Exception e) {
        }
        return rePreMapper.toDto(save);
    }

    public void reigstRace(String realName, String phone) {
        String url = "https://zqx.fzzgsc.com/api/sys/user/batchRegisterWithOutSendMsg";
        String jsonStr = "{\n" +
                "    \"list\": [\n" +
                "        {\n" +
                "            \"realName\": \""+realName+"\",\n" +
                "            \"phone\": \""+phone+"\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        HttpUtils2.post(url, jsonStr);
    }
}